package com.wanglibao.woodtrade.woodtrade.ui.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.config.NetConfig;
import com.wanglibao.woodtrade.woodtrade.jsonrpc.JsonRPCAsyncTask;
import com.wanglibao.woodtrade.woodtrade.ui.activity.CommodityActivity;
import com.wanglibao.woodtrade.woodtrade.ui.activity.SupplyAndDemendActivity;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter.HomeFragmentRecommendAdapter;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.adapter.model.Commodity;
import com.wanglibao.woodtrade.woodtrade.util.PullUtil;
import com.wanglibao.woodtrade.woodtrade.util.ToastUtil;
import com.wanglibao.woodtrade.woodtrade.widget.PullToRefreshView.PullToRefreshBase;
import com.wanglibao.woodtrade.woodtrade.widget.PullToRefreshView.PullToRefreshListView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lugg on 2016/5/25.
 */
public class CommodityFragment extends BaseFragment implements AdapterView.OnItemClickListener, PullToRefreshListView.OnRefreshListener2 {

    public int pageIndex = 1;

    @Bind(R.id.list_fragment_commodity_data)
    PullToRefreshListView list_fragment_commodity_data;

    @Bind(R.id.ll_fragment_commodity_null)
    LinearLayout ll_fragment_commodity_null;

    HomeFragmentRecommendAdapter homeFragmentRecommendAdapter;

    public String keyWord = "";
    public String category_id = "";
    public String category_one_id = "";
    public String woodTypeDetailThreePlaceId = "";


    Commodity commodity;
    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.arg2 == 2) {
                if (pageIndex == 1){
                    if (commodity != null && commodity.getList() != null){
                        commodity.getList().clear();
                    }
                    homeFragmentRecommendAdapter.update(commodity);
                    if (ll_fragment_commodity_null.getVisibility() == View.GONE){
                        ll_fragment_commodity_null.setVisibility(View.VISIBLE);
                        list_fragment_commodity_data.setVisibility(View.GONE);
                    }
                }else{
                    ToastUtil.createNormalToast("没有更多数据了");
                    pageIndex--;
                }

            }else{
                Commodity commodity1 = new Gson().fromJson(msg.obj.toString(), Commodity.class);
                if (pageIndex == 1) {
                    commodity = commodity1;
                } else {
                    commodity.getList().addAll(commodity1.getList());
                }
                homeFragmentRecommendAdapter.update(commodity);
                if (ll_fragment_commodity_null.getVisibility() == View.VISIBLE){
                    ll_fragment_commodity_null.setVisibility(View.GONE);
                    list_fragment_commodity_data.setVisibility(View.VISIBLE);
                }
            }
            list_fragment_commodity_data.onRefreshComplete();
            return false;
        }
    });

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_commodity, container, false);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        category_one_id = getArguments().getString("woodTypeId");
        loadData();

        commodity = new Commodity();
        commodity.setList(new ArrayList<Commodity.ListBean>());
        homeFragmentRecommendAdapter = new HomeFragmentRecommendAdapter(getContext(), commodity);
        list_fragment_commodity_data.getRefreshableView().setAdapter(homeFragmentRecommendAdapter);
        list_fragment_commodity_data.getRefreshableView().setOnItemClickListener(this);
        list_fragment_commodity_data.setMode(PullToRefreshBase.Mode.BOTH);
        list_fragment_commodity_data.getRefreshableView().setSelector(new ColorDrawable(Color.TRANSPARENT));
        list_fragment_commodity_data.setOnRefreshListener(this);
        PullUtil.initIndicator(list_fragment_commodity_data);
    }
    //SERVER_wangxiaoming
    public void loadData() {
        JsonRPCAsyncTask jsonRPCAsyncTask = new JsonRPCAsyncTask(false,this, NetConfig.SERVER + NetConfig.TYPE_GOODS,
                NetConfig.lists, handler,
                "jsonObject", NetConfig.TaskId.lists,
                new Object[]{pageIndex, keyWord, category_id, category_one_id, woodTypeDetailThreePlaceId, ""});
        jsonRPCAsyncTask.execute(new Void[]{});
    }

    @OnClick(R.id.tv_layout_null_sentInfo) void sentInfoClick(){
        startActivity(new Intent(getActivity(), SupplyAndDemendActivity.class));
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(), CommodityActivity.class);
        intent.putExtra("id",commodity.getList().get(position-1).getGoods_id());
        startActivity(intent);
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        pageIndex = 1;
        loadData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        pageIndex++;
        loadData();
    }
}
